package com.hnevc.jsondemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public EditText username,password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //将资源文件R.raw.data转换为字节输入流
        InputStream inputStream = getResources().openRawResource(R.raw.data);

        //将字节输入流转换为字符串
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(reader);

        //字符串缓存
        StringBuffer sb = new StringBuffer();
        try {
            String s="";
            while (s!=null) {
                sb.append(s);
                s = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String data = sb.toString();
        Log.e("a", "onCreate: "+data );

        //解析data
        Gson gson=new Gson();
        List<Food> foodList=gson.fromJson(data,new TypeToken<List<Food>>(){}.getType());

        MyAdapter myAdapter = new MyAdapter(foodList, this);
        ListView foodListView=(ListView)findViewById(R.id.id_lv_food);
        foodListView.setAdapter(myAdapter);
    }
}
